// https://juejin.cn/post/7185098616137056313

import express from 'express';
import multer from 'multer';
import MAO from 'multer-aliyun-oss';
import config from 'config';

console.log('--------process.env.NODE_ENV--------', process.env.NODE_ENV);
console.log('--------config-++++-------', config);

const router = express.Router();

const upload = multer({
  storage: MAO({
    config: config.get('oss'),
    destination(req, file, cb) {
      // 这里可以访问 req
      const folder = req.query.destination || 'ticket/images';
      cb(null, folder);
    }
  })
});

router.post('/file/upload', upload.single('file'), (req, res) => {
  // 可以自定义返回结果，推荐打印 req.file 查看，再决定如何返回数据给前端
  const { file } = req;
  console.log('/file/upload', file);
  res.sendOk(file);
});

export default router;
